package com.cloud.bc.controller.ajax;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.entity.BcArticles;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.BcArticlesService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/data")
public class Ajax_DataController {

    private final BcArticlesService bcArticlesService;

    @GetMapping("article")
    public AjaxResp article(Integer cId,
                            Integer page) {
        AjaxResp ajaxResp = new AjaxResp();
        Page p = new Page();
        p.setCurrent(null == page ? 1 : page);
//        p.setSize(1);//临时测试使用
        BcArticles bcArticles = new BcArticles();
        if (Convert.toInt(cId, 0) > 0) {
            bcArticles.setCategoryId(cId);
        }
        IPage<BcArticles> bcArticlesIPage = bcArticlesService.selectPage(p, bcArticles);
        List<BcArticles> records = bcArticlesIPage.getRecords();
        records.forEach(item -> {
            if (StrUtil.isEmpty(item.getCover())) {
                //暂无图片
            } else {
                item.setCover(item.getCover() + "_200x200.jpg");
            }
            item.setContent("");
        });
        ajaxResp.setList(records);
        return ajaxResp;
    }
}
